﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Z2.Core
{
	public class ParticleItem : ParticleBasic
	{

		#region Members

		private ItemBase m_item;

		#endregion

		#region Constructor

		public ParticleItem(ItemBase item)
			: base(item.Sprite.Alias)
		{
			this.Item = item;
		}

		#endregion

		#region Properties

		public ItemBase Item
		{
			get { return this.m_item; }
			protected set { this.m_item = value; }
		}

		#endregion

		#region Override Methods

		public override void Draw(SpriteBatch batch)
		{
			if (this.Item != null) this.Item.Draw(batch);
		}

		protected override void ModifyVector()
		{
			base.ModifyVector();

			if (this.Item != null) this.Item.Sprite.Angle = this.Sprite.Angle;
		}

		protected override void OnPositionChanged()
		{
			base.OnPositionChanged();
			if (this.Item != null && !this.Item.IsPickedUp) this.Item.Position = this.Position;
		}

		public override void Initialize()
		{
			base.Initialize();

			this.Item.Initialize();
			this.Item.IsSwinging = false;
			this.Item.Sprite.StartAnimation();
			this.Item.Destroyed += this.Item_Destroyed;
			World.Current.RegisterObject(this.Item);
		}

		#endregion

		#region Event Handling

		private void Item_Destroyed(object sender, EventArgs e)
		{
			this.Item.Destroyed -= this.Item_Destroyed;
			this.Item = null;
			this.Destroy();
		}

		#endregion

	}
}
